<?php
class country extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($id) {
			$where .= " AND id='{$id}'";
		}
		if ($keyword) {
			$where .= " AND (name LIKE '%{$keyword}%' OR name_english LIKE '%{$keyword}%' OR memo LIKE '%{$keyword}%')";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));

		$sql = "SELECT * FROM {$this->tbp}country WHERE 1 {$where} ORDER BY id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);

		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->display();
		}
	}

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}country WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign('row', $row);
			$this->display();
		}
	}

	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$name_english = (isset($_POST['name_english']) && trim($_POST['name_english'])) ? trim($_POST['name_english']) : '';
		$memo = (isset($_POST['memo']) && trim($_POST['memo'])) ? trim($_POST['memo']) : '';
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;

		$detail_pic = upload_one_file("country","pic");
		$list_pic_big = upload_one_file("country","pic_big");
		$list_pic_small = upload_one_file("country","pic_small");
		$flag_pic = upload_one_file("country","flag_pic");
		if (!$detail_pic) $detail_pic = (isset($_POST['origin_pic']) && trim($_POST['origin_pic'])) ? trim($_POST['origin_pic']) : '';
		if (!$list_pic_big) $list_pic_big = (isset($_POST['origin_pic_big']) && trim($_POST['origin_pic_big'])) ? trim($_POST['origin_pic_big']) : '';
		if (!$list_pic_small) $list_pic_small = (isset($_POST['origin_pic_small']) && trim($_POST['origin_pic_small'])) ? trim($_POST['origin_pic_small']) : '';
		if (!$flag_pic) $flag_pic = (isset($_POST['origin_flag_pic']) && trim($_POST['origin_flag_pic'])) ? trim($_POST['origin_flag_pic']) : '';
		if ($id > 0) {  //edit
			$sql = "UPDATE {$this->tbp}country SET name='{$name}', name_english='{$name_english}', sort='{$sort}' ,
				status='{$status}', memo='{$memo}'
				WHERE id='{$id}'";
			$act = $this->db->query($sql);
			//更新图片
			if($detail_pic !== '' || $list_pic_big !== '' || $list_pic_small !== '' || $flag_pic !== ''){
				$thing = '';
				if($detail_pic){
					$thing .= " detail_pic='{$detail_pic}',";
				}
				if($list_pic_big){
					$thing .= " list_pic_big='{$list_pic_big}',";
				}
				if($list_pic_small){
					$thing .= " list_pic_small='{$list_pic_small}',";
				}
				if($flag_pic){
					$thing .= " flag_pic='{$flag_pic}',";
				}
				$thing=substr($thing,0,strlen($thing)-1);
				$sql = "UPDATE {$this->tbp}country SET {$thing} where id = '{$id}'";
				$this->db->query($sql);
			}
		} else { //add
			$sql = "INSERT INTO {$this->tbp}country(name, name_english, memo ,detail_pic,list_pic_big,list_pic_small,flag_pic,
				sort, add_time, status) VALUES('{$name}', '{$name_english}','{$memo}', '{$detail_pic}', '{$list_pic_big}',
				'{$list_pic_small}','{$flag_pic}','{$sort}', '".time()."', '{$status}')";
			$this->db->query($sql);
		}
		header("Location:?app=country&act=index");
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}country WHERE id='{$id}'";
		if($this->db->query($sql)){	//删除该商品的收藏
			$sql="DELETE FROM {$this->tbp}favorite WHERE item_id='{$id}' AND type_id = 3";
			$this->db->query($sql);
		}
		header("Location:?app=country&act=index");
	}
}
